#include "ServiceGetDataValues.h"

CServiceGetDataValues::CServiceGetDataValues(void)
{
    ServiceName();
}

CServiceGetDataValues::~CServiceGetDataValues(void)
{
    delete m_CurrentItem;
    m_CurrentItem=NULL;
}
void CServiceGetDataValues::ServiceName()
{
    printf("Service:GetDataValues\n");
}
BOOL CServiceGetDataValues::SetTreeItem(QTreeWidgetItem *treeItem)
{
    m_CurrentItem=treeItem;
    m_treeItem=new CTreeItem(treeItem);
    return TRUE;
}
BOOL CServiceGetDataValues::OnGetDataValues(const char * szDataRef, CMTree & tree)
{
    printf("OnGetDataValues %s\n",szDataRef);
    PrintTree(tree);
    return TRUE;
}
void CServiceGetDataValues::PrintTree(CMTree & tree)
{
    int i, level;
    int up;
    BOOL ret;
    CAcsiVar var;

    ret = tree.GotoRoot();
    i = 0;
    level = 1;
    up = 0;

    while(ret == TRUE)
    {

        if(up != 0)
        {
            printf("}");

            QString strValue;
            strValue.append("{");
            for (int nn=0;nn<m_treeItem->GetCurrentItem()->childCount();nn++)
            {
                strValue.append(m_treeItem->GetCurrentItem()->child(nn)->text(1));
                if (nn==(m_treeItem->GetCurrentItem()->childCount()-1))
                {
                    strValue.append(" ");
                }
                else
                {
                    strValue.append(",");
                }
            }
            strValue.append("}");
            m_treeItem->GetCurrentItem()->setText(1,strValue);

            //printf("\n");
            ret = tree.GotoNext();
            m_treeItem->GetNext();
            if(ret == TRUE)
            {
                up = 0;
            }
            else
            {
                ret = tree.GotoParent();
                m_treeItem->GetParent();
                level--;
            }
        }
        else if(!tree.IsLeaf())
        {
            if(tree.pNode->len > 0)
            {
                var = (AcsiVariant *)tree.pNode->data;
                PrintVar(var);
            }

            level++;
            printf("{");
            //printf("\n");
            ret = tree.GotoChild();
            m_treeItem->GetChild();
        }
        else
        {
            var = (AcsiVariant *)tree.pNode->data;
            PrintVar(var);
            //printf("\n");
            ret = tree.GotoNext();
            m_treeItem->GetNext();
            
            if(!ret)
            {
                up = 1;
                ret = tree.GotoParent();
                m_treeItem->GetParent();
                level--;
            }
        }
    }
    printf("\n");
}
void CServiceGetDataValues::PrintType(int type, int size)
{
    printf("  (");

    switch(type)
    {
    case BOOL_CODE:
        printf("bool");
        break;
    case BITS_CODE:
        printf("bstr%d", size);
        break;
    case INT_CODE:
        if(size == 1)
        {
            printf("char");
        }
        else if(size == 2)
        {
            printf("short");
        }
        else if(size == 4)
        {
            printf("int");
        }
        else
        {
            printf("int%d", size);
        }
        break;
    case UINT_CODE:
        if(size == 1)
        {
            printf("byte");
        }
        else if(size == 2)
        {
            printf("word");
        }
        else if(size == 4)
        {
            printf("uint");
        }
        else
        {
            printf("uint%d", size);
        }
        break;
    case FLOAT_CODE:
        if(size == 5)
        {
            printf("float");
        }
        else if(size == 9)
        {
            printf("double");
        }
        else
        {
            printf("float%d", size);
        }
        break;
    case OCT_CODE:
        printf("oct%d", size);
        break;
    case VSTR_CODE:
        printf("vstr%d", size);
        break;
    case BTIME_CODE:
        printf("btime%d", size);
        break;
    case UTF8_STR_CODE:
        printf("utf8str%d", size);
        break;
    case UTCTIME_CODE:
        printf("utc");
        break;
    default:
        printf("%d, %d", type, size);
        break;
    }

    printf(")", size);
}

void CServiceGetDataValues::PrintVar(CAcsiVar & var)
{
    int i;
    struct tm gm;
    char * name;
    unsigned char * data;
    TimeStamp * time;

    //name = var.GetName();

    //if(name != NULL)
    //{
    //    printf(name);

    //    if(var.GetMmsType() >= 0)
    //    {
    //        PrintType(var.GetMmsType(), var.GetMmsSize());
    //    }

    //    return;
    //}
    
        //  if(var.GetVarSize() > 0){
        switch(var.GetVarType())
        {
        case BDT_BOOLEAN:
            if(var.GetBool() == 0)
            {
                printf(" Bool:  F");
                m_treeItem->GetCurrentItem()->setText(1,"Bool:  F");
            }
            else
            {
                printf(" Bool:  T");
                m_treeItem->GetCurrentItem()->setText(1,"Bool:  T");
            }
            break;
        case BDT_INT8:
        case BDT_INT16:
        case BDT_INT32:
            {
                printf(" int:  %d", (int)var);
                char tmp[20];
                sprintf(tmp," int:%d ",(int)var);
                m_treeItem->GetCurrentItem()->setText(1,QLatin1String(tmp));
            }
            break;
        case BDT_INT8U:
        case BDT_INT16U:
        case BDT_INT32U:
            {
                printf(" uint:  %d", (unsigned int)var);
                char tmp[20];
                sprintf(tmp," uint:%d ",(unsigned int)var);
                m_treeItem->GetCurrentItem()->setText(1,QLatin1String(tmp));
            }
            break;
        case BDT_FLOAT32:
        case BDT_FLOAT64:
            {
                printf(" float:  %f", (double)var);
                char tmp[20];
                sprintf(tmp," float:%f ",(double)var);
                m_treeItem->GetCurrentItem()->setText(1,QLatin1String(tmp));
            }
            break;
        case BDT_OCTETSTRING:
            {
                QString str;
                printf(" oct:  ");
                str.append("oct: ");
                data = var.GetOctStr();
                for(i = 0; i < var.GetOctLen(); i++)
                {
                    //char tmp[20];
                    //memset(tmp,"\0",20);
                    printf("%02x ", data[i]);
                    //printf(tmp,"%02x ", data[i]);
                    str.append(QString("%1").arg(data[i],2,16,QLatin1Char('0')));
                }
                m_treeItem->GetCurrentItem()->setText(1,str);
            }
            break;
        case BDT_VISIBLESTRING:
            {
                printf(" str:  %s", (char *)var);
                char tmp[20];
                sprintf(tmp," str:%s ",(char *)var);
                m_treeItem->GetCurrentItem()->setText(1,QLatin1String(tmp));
            }
            break;
        case BDT_UNICODESTRING:
            {
                char str[256];
                strcpy(str, "");
    #ifdef WIN32
                WCHAR wstr[512];
                wstr[0] = 0;
                MultiByteToWideChar(CP_UTF8, 0, (char *)(var.GetUtf8Str()), -1, wstr,
                    sizeof(wstr));
                WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, sizeof(str), NULL, NULL);
    #endif
                printf(" utf8str:  %s", str);
                char tmp[20];
                sprintf(tmp," utf8str:%s ",str);
                m_treeItem->GetCurrentItem()->setText(1,QLatin1String(tmp));
            }
            break;
        case BDT_BITSTRING:
            {
                QString str;
                printf(" bits:  ");
                str.append("bits:[");
                data = var.GetBitStr();
                for(i = 0; i < var.GetBitLen(); i++)
                {
                    if((data[i / 8] & (1 << (7 - i % 8))) == 0)
                    {
                        printf("0");
                        str.append("0");
                    }
                    else
                    {
                        printf("1");
                        str.append("1");
                    }
                }
                str.append("]");
                m_treeItem->GetCurrentItem()->setText(1,str);
            }
            break;
        case BDT_TimeStamp:
            {
                //          var.GetTime().secs -= 28800 * 2;
                time = &var.GetTime();
                gm = * localtime(&time->secs);
                printf("time:  %04d-%02d-%02d %02d:%02d:%02d %03d  %x",
                    gm.tm_year + 1900, gm.tm_mon + 1, gm.tm_mday, gm.tm_hour, gm.tm_min, gm.tm_sec,
                    GetTimeStampMsec(time), time->qflags);
                //              (int)((1000.0 * var.GetTime().fraction) / 0x00ffffff), var.GetTime().qflags);
                char tmp[100];
                sprintf(tmp," time:  %04d-%02d-%02d %02d:%02d:%02d %03d  %x",
                    gm.tm_year + 1900, gm.tm_mon + 1, gm.tm_mday, gm.tm_hour, gm.tm_min, gm.tm_sec,
                    GetTimeStampMsec(time), time->qflags);
                m_treeItem->GetCurrentItem()->setText(1,QLatin1String(tmp));
            }
            break;
        default:
            PrintType(var.GetMmsType(), var.GetMmsSize());
            break;
        }

}


